<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>{{ detailName }}区角详情</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <!--        内容-->
        <el-tabs v-model="activeName" @tab-click="handleClick" type="card">
          <el-tab-pane label="基本设置" name="first">
            <Basicsettings @getModleVal="getModleVal" :editDetail="detaiOption" :adminUser="adminUser"
                           :editId="cornerId" :cateData="cateData"></Basicsettings>
          </el-tab-pane>
          <!--          <el-tab-pane label="评价规则" name="second">-->
          <!--            <EvaluationRules @getModleVal="getModleVal" :cornerId="cornerId" :assess="assess"-->
          <!--                             :studyIds="studyIds"></EvaluationRules>-->
          <!--          </el-tab-pane>-->
          <el-tab-pane label="预约班级" name="third">
            <BookingClass @getModleVal="getModleVal" :detailClass="detailClass"></BookingClass>
          </el-tab-pane>
          <!--          <el-tab-pane label="区角设备" name="fourth">-->
          <!--            <deviceManagement @getModleVal="getModleVal" :detailDevice="detailDevice"></deviceManagement>-->
          <!--          </el-tab-pane>-->
          <el-tab-pane label="管理人员" name="five">
            <Collector @getModleVal="getModleVal" :adminUserList="adminUserList"></Collector>
          </el-tab-pane>
        </el-tabs>
      </div>
      <div class="cornerComment-in-footer">
        <el-button type="primary" @click="submitForm" size="small">确定并提交</el-button>
        <el-button @click="banckline" size="small">全部退出</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import Basicsettings from "./editTab/Basicsettings";
import BookingClass from "./editTab/BookingClass";
import Collector from "./editTab/Collector";
import EvaluationRules from "./editTab/EvaluationRules";
import deviceManagement from "./editTab/deviceManagement";

export default {
  props: ['visible', 'cornerId'], // commoent 学生点评 collection 学生采集
  components: {
    Basicsettings,
    BookingClass,
    Collector,
    EvaluationRules,
    deviceManagement
  },
  data() {
    return {
      dialogVisible: false,
      activeName: 'first',
      detailName: '',
      editForm: {
        number: '', // 可参与的人数
        pic: '', // 现场图
        name: '', // 区角名称
        section_id: '', // 年段
        class_id: '', // 班级
        user_id: '', //负责人id
        facility_id: 0, // 识别器
        time: '', // 达标时长
        list: '', // 评分规则
        base_score: '', // 基本分值
        join_class: '', // 可预约班级
        comment_user: '', // 管理人员
        description: '', // 设置目标
        study_ids: '',
        cate_id: '',
        open_week: '',
        position_id: 1,
        themes:[]
      }, // 提交数据
      detaiOption: '',
      adminUser: '',  // 负责人员
      adminUserList: '', // 管理人员
      detailClass: '', // 预约班级
      cateData: null,
      flag: true
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.cornerId) {
          this.getDetailCorner() // 获取详情数据
        }
      } else {
        this.activeName = 'first'
        this.editForm = {
          number: '', // 可参与的人数
          pic: '', // 现场图
          name: '', // 区角名称
          section_id: '', // 年段
          class_id: '', // 班级
          user_id: '', //负责人id
          facility_id: 0, // 识别器
          time: '', // 达标时长
          list: '', // 评分规则
          base_score: '', // 基本分值
          join_class: '', // 可预约班级
          comment_user: '', // 管理人员
          description: '', // 设置目标
          study_ids: '',
          cate_id: '',
          open_week: '',
          position_id: 1,
          themes:[]
        } // 提交数据
        this.detaiOption = ''
        this.adminUser = ''  // 负责人员
        this.adminUserList = '' // 管理人员
        this.detailClass = '' // 预约班级
        this.cateData = null
      }
    }
  },
  created() {
  },
  methods: {
    // 初始化
    async getDetailCorner() {
      let data = await this.$fetch(this.api.admin_corner_edit, {
        id: this.cornerId
      });
      let newlist = {}
      for (let i in this.editForm) {
        newlist[i] = data.vo[i]
      }
      newlist.cate_id = newlist.cate_id.toString()
      this.cateData = data.vo.cate
      this.detailName = data.vo.name
      this.detaiOption = this.getJson(newlist)
      this.adminUser = this.getJson(data.vo.user)
      this.detailClass = this.getJson(data.vo.join_class)
      this.adminUserList = this.getJson(data.vo.comment_user)
      this.editForm.themes = JSON.parse(data.vo.themes)
      console.log('this.editForm.themes:', this.editForm.themes)
    },
    submitForm(formName) {
      if (!this.editForm.pic) {
        this.$message.error('请上传现场图')
        return
      }

      if (!this.editForm.name) {
        this.$message.error('区角名称不能为空')
        return
      }
      if (!this.editForm.cate_id) {
        this.$message.error('选择区角类目')
        return
      }
      if (!this.editForm.section_id || !this.editForm.class_id) {
        this.$message.error('完善区角位置信息')
        return
      }
      if (!this.editForm.open_week) {
        this.$message.error('请选择开放时间')
        return
      }
      if (this.editForm.open_week && !this.editForm.open_week.length) {
        this.$message.error('请选择开放时间')
        return
      }
      if (!this.editForm.time) {
        this.$message.error('填写达标时长')
        return
      }
      if (!this.editForm.number) {
        this.$message.error('填写参与人数')
        return
      }
      if (Number(this.editForm.base_score) <= 0) {
        this.$message.error('基本分值需大于0')
        return
      }
      // 属性评分判断
      if (!this.editForm.join_class) {
        this.$message.error('选择可预约班级')
        return
      }

      this.submitFormAdd()
    },
    async submitFormAdd() {
      let that = this
      let option = this.getJson(this.editForm)
      if (this.cornerId) {
        option.id = this.cornerId
      }
      option.open_week = option.open_week.join(',')
      console.log('数据内容：', option)
      if(!this.flag){
        return
      }
      this.flag = false
      let data = await this.$post(this.cornerId ? this.api.admin_corner_edit : this.api.admin_corner_add, option)
      console.log(data)
      if (!data.code) {
        this.$message.success(data.msg)
        this.$emit('InitFn')
        this.closePOpup()
      } else {
        this.$message.error(data.msg)
      }
      setTimeout(()=>{
        that.flag = true
      }, 1500)
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    banckline() {
      this.$confirm('是否退出所有编辑？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.closePOpup()
      }).catch(() => {
      })
    },
    // 参数变更进行更正
    getModleVal(option) {
      if (option.type === 'form') {
        for (let i in option.data) {
          this.editForm[i] = option.data[i]
        }
      }
      //品质
      if (option.type == 'study_ids') {
        this.editForm.study_ids = option.data.join(',')
      }
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    handleClick(tab, event) {
      console.log(tab, event);
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 60%;
  height: 70%;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.toolbarLine {
  text-align: center;
}
</style>


